package com.tomato.dto;

import com.tomato.entity.TodoSetting;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Data
@Schema(description = "待办设定同步传输对象")
public class TodoSettingSyncDTO {

    @Schema(description = "客户端本地创建时间（ISO格式）",
            example = "2023-08-01T10:00:00",
            required = true)
    private LocalDateTime clientCreatedAt;

    @Schema(description = "客户端本地最后修改时间（ISO格式）",
            example = "2023-08-05T15:30:00",
            required = true)
    private LocalDateTime clientUpdatedAt;

    @Schema(description = "待办标题/名称",
            example = "完成项目文档编写",
            required = true,
            maxLength = 255)
    private String title;

    @Schema(description = "类型: POMODORO=普通番茄钟；GOAL=定目标",
            example = "POMODORO",
            required = true)
    private TodoSetting.Type type;

    @Schema(description = "计时模式: COUNTDOWN=倒计时；UPTIME=正向计时；NO_TIMER=不计时",
            example = "COUNTDOWN",
            required = true)
    private TodoSetting.TimerMode timerMode;

    @Schema(description = "倒计时总时长（秒），仅timerMode=COUNTDOWN时有效",
            example = "1500")
    private Integer countdownDuration;

    @Schema(description = "目标截止日期（yyyy-MM-dd），仅type=GOAL时有效",
            example = "2023-12-31")
    private String goalDeadline;

    @Schema(description = "目标类型: DURATION=时长目标；TIMES=次数目标，仅type=GOAL时有效",
            example = "DURATION")
    private TodoSetting.GoalTargetType goalTargetType;

    @Schema(description = "目标值：时长（秒）或次数，仅type=GOAL时有效",
            example = "7200")
    private Integer goalTargetValue;

    @Schema(description = "休息时长（秒），默认300",
            example = "300",
            defaultValue = "300")
    private Integer restDuration = 300;

    @Schema(description = "循环次数，默认1",
            example = "1",
            defaultValue = "1")
    private Integer cycle = 1;

    /**
     * 转换为实体对象
     *
     * @param userId     所属用户ID
     * @param uploadedAt 服务端上传时间
     * @return 转换后的实体对象
     */
    public TodoSetting toEntity(Long userId, LocalDateTime uploadedAt) {
        TodoSetting setting = new TodoSetting();
        setting.setUserId(userId);
        setting.setTitle(this.title);
        setting.setType(this.type);
        setting.setTimerMode(this.timerMode);
        setting.setCountdownDuration(this.countdownDuration);

        // 处理目标相关字段
        if (this.goalDeadline != null) {
            setting.setGoalDeadline(LocalDate.parse(this.goalDeadline));
        }
        setting.setGoalTargetType(this.goalTargetType);
        setting.setGoalTargetValue(this.goalTargetValue);

        // 设置时间相关字段
        setting.setCreatedAt(this.clientCreatedAt != null ?
                this.clientCreatedAt : LocalDateTime.now());
        setting.setUpdatedAt(this.clientUpdatedAt != null ?
                this.clientUpdatedAt : LocalDateTime.now());
        setting.setUploadedAt(uploadedAt);

        // 设置默认值字段
        setting.setRestDuration(this.restDuration != null ?
                this.restDuration : 300);
        setting.setCycle(this.cycle != null ? this.cycle : 1);

        return setting;
    }
}